

package com.appview;

import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.model.StuException;
import com.model.Teacher;
import com.util.Config;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

/*
 * 登录界面
 */

public class StuLogin extends JFrame implements ActionListener, KeyListener{
    private JPanel jPanel, jPanel1, jPanel2, jPanel3, jPanel4, jPanel5;
    private JLabel jLabel1, jLabel2, jLabel3, JLabel4;
    private JButton jButton1, jButton2, jButton3;
    private JTextField jField1;
    private JPasswordField jPasswordField;
    private JRadioButton rButton1, rButton2, rButton3;
    private JCheckBox jBox1, jBox2, jBox3;
    private ButtonGroup bGroup;
    private JList jList;

    public static void main(String[] args) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StuLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StuLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StuLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StuLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        new StuLogin();
    }
    public StuLogin() {
        //设置文字
        jLabel1 = new JLabel("账号:");
        jLabel2 = new JLabel("密码:");
        jLabel3 = new JLabel("欢迎使用教学信息管理系统");
        JLabel4 = new JLabel("选择管理类型");

        //设置文本框
        jField1 = new JTextField(13);
        jPasswordField = new JPasswordField(13);
        jField1.addKeyListener(this);
        jPasswordField.addKeyListener(this);
//		//设置复选框
//		jBox1 = new JCheckBox("记住密码");
//		jBox2 = new JCheckBox("自动登录");

        //设置单选框
        rButton1 = new JRadioButton("管理员");
        rButton2 = new JRadioButton("教师");
        rButton3 = new JRadioButton("学生");
        rButton3.setSelected(true);


        //设置单选框只能单选
        bGroup = new ButtonGroup();
        bGroup.add(rButton1);
        bGroup.add(rButton2);
        bGroup.add(rButton3);

        rButton1.addKeyListener(this);
        rButton2.addKeyListener(this);
        rButton3.addKeyListener(this);

        //设置按钮
        jButton1 = new JButton("登录");
        jButton2 = new JButton("退出");
        jButton3 = new JButton("重置");
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);

        //添加组件
        jPanel = new JPanel();
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jPanel4 = new JPanel();
        jPanel5 = new JPanel();
        jPanel3 = new JPanel();

        jPanel.add(jLabel3);
        jPanel1.add(jLabel1, BorderLayout.WEST);
        jPanel1.add(jField1, BorderLayout.CENTER);
        jPanel2.add(jLabel2, BorderLayout.WEST);
        jPanel2.add(jPasswordField, BorderLayout.CENTER);

//		jPanel4.add(jBox1);
//		jPanel4.add(jBox2);
//		jPanel4.setLayout(new FlowLayout(FlowLayout.CENTER));

        jPanel5.add(rButton1);
        jPanel5.add(rButton2);
        jPanel5.add(rButton3);
        jPanel5.setLayout(new FlowLayout(FlowLayout.CENTER));

        jPanel3.add(jButton1);
        jPanel3.add(jButton2);
        jPanel3.add(jButton3);
        jPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));

        //添加组件
        this.add(jPanel);
        this.add(jPanel1);
        this.add(jPanel2);
//		this.add(jPanel4);
        this.add(jPanel5);
        this.add(jPanel3);
        this.setLayout(new GridLayout(5, 1));

        //设置界面
        this.setTitle("登录系统");
        this.setSize(310, 270);
        this.setLocation(450, 250);
        this.setVisible(true);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
    }

    //按钮监听
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {
            if (rButton2.isSelected()) {
                //选中教师
                Config.setLoginType(0);//教师为0
            } else if (rButton3.isSelected()) {
                //选中学生
                Config.setLoginType(1);//学生为1
            } else if (rButton1.isSelected()) {
                Config.setLoginType(2); //管理员为2
            }
            if (isLogin()) { //验证账号密码
                if (Config.getLoginType() == 2) {
                    new ManagerSelect().openView();
                }else {
                    new UserView(jField1.getText().toString()).openView();
                }
                this.dispose();
            }
        }else if (e.getSource() == jButton2) {
            StuLogin.this.dispose();
        }else if (e.getSource() == jButton3) {
            jField1.setText("");
            jPasswordField.setText("");
        }
    }


    //验证账号密码
    private boolean isLogin() {
        String accout = jField1.getText();
        String pwd = jPasswordField.getText();
        TeacherDao teacherDao = TeacherDao.getInstance();
        StudentDao studentDao = StudentDao.getInstance();
        try {
            if (Config.getLoginType() == 0) {
                teacherDao.login(accout, pwd);
            } else if (Config.getLoginType() == 1) {
                studentDao.login(accout, pwd);
            } else if (Config.getLoginType() == 2) {
                if (!accout.equals(Config.ROOT)||!pwd.equals(Config.PASSWORD)){
                    throw new StuException("用户名或密码不正确！");
                }
            }
            return true;
        } catch (StuException e){
            JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        return false;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            System.out.println("1111");
            jButton1.doClick();
        } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            System.out.println("117");
            this.dispose();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
